package com.ruoyi.fts.wxconfig;

import com.wechat.pay.java.core.Config;
import com.wechat.pay.java.core.RSAAutoCertificateConfig;
import com.wechat.pay.java.service.payments.jsapi.JsapiServiceExtension;
import com.wechat.pay.java.service.refund.RefundService;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class WxPayInit {


    public static String APPID;
    public static String active;
    public static String SECRET;
    /** 商户号 */
    public static String merchantId;
    /** 商户API私钥路径 */
    public static String privateKeyPath;
    /** 商户证书序列号 */
    public static String merchantSerialNumber;
    /** 商户APIV3密钥 */
    public static String apiV3Key;
    /** 支付异步回调地址 */
    public static String NOTIFYURL;
    @Value("${wx.mchid}")
    public void setMerchantId(String merchantId) {
        WxPayInit.merchantId = merchantId;
    }
    @Value("${wx.privateKeyPath}")
    public void setPrivateKeyPath(String privateKeyPath) {
        WxPayInit.privateKeyPath = privateKeyPath;
    }
    @Value("${wx.merchantSerialNumber}")
    public void setMerchantSerialNumber(String merchantSerialNumber) {
        WxPayInit.merchantSerialNumber = merchantSerialNumber;
    }
    @Value("${wx.apiV3Key}")
    public void setApiV3Key(String apiV3Key) {
        WxPayInit.apiV3Key = apiV3Key;
    }

    @Value("${spring.profiles.active}")
    public void setActive(String active) {
        WxPayInit.active = active;
    }
    @Value("${wx.appid}")
    public void setAPPID(String APPID) {
        WxPayInit.APPID = APPID;
    }
    @Value("${wx.secret}")
    public void setSECRET(String SECRET) {
        WxPayInit.SECRET = SECRET;
    }
    @Value("${wx.notifyUrl}")
    public void setNOTIFYURL(String NOTIFYURL) {
        WxPayInit.NOTIFYURL = NOTIFYURL;
    }
    /*************************************************************/
    public static JsapiServiceExtension service;
    public static RefundService refundService;
    @PostConstruct
    public static void init() throws Exception {
        System.err.println("************************微信支付初始化商户配置****************************");

        try {
            // 初始化商户配置
            Config config = new RSAAutoCertificateConfig.Builder()
                    .merchantId(WxPayInit.merchantId)
                    // 使用 com.wechat.pay.java.core.util 中的函数从本地文件中加载商户私钥，商户私钥会用来生成请求的签名
                    .privateKeyFromPath(WxPayInit.privateKeyPath)
                    .merchantSerialNumber(WxPayInit.merchantSerialNumber)
                    .apiV3Key(WxPayInit.apiV3Key)
                    .build();
            // 初始化服务
            service = new JsapiServiceExtension.Builder()
                    .config(config)
                    .signType("RSA") // 不填默认为RSA
                    .build();
            refundService=new RefundService.Builder()
                    .config(config)
                    .build();
            System.err.println("************************微信支付初始化商户配置成功****************************");
        }catch (Exception e){
            System.err.println("************************微信支付初始化商户配置失败****************************");
            System.err.println(e.getMessage());

            //throw new Exception(e.getMessage());
        }

    }
}
